import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * @author wjj
 */
public class PatchedJsonGenerator {

    private static final String PATCHED_FORM_DATA = "{\"map_policy\":[{\"marketplace_id\":\"\",\"value\":\"policy_10\"}],\"purchasable_offer\":[{\"map_price\":[{\"schedule\":[{\"value_with_tax\":2}]}],\"currency\":\"sdds\",\"marketplace_id\":\"\",\"our_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"minimum_seller_allowed_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"maximum_seller_allowed_price\":[{\"schedule\":[{\"value_with_tax\":3}]}],\"discounted_price\":[{\"schedule\":[{\"end_at\":\"2025-02-10\",\"start_at\":\"2025-02-10\",\"value_with_tax\":7}]}],\"start_at\":{\"value\":\"2025-02-10\"},\"end_at\":{\"value\":\"2025-02-28\"},\"audience\":\"ALL\",\"quantity_discount_plan\":[{\"schedule\":[{\"discount_type\":\"fixed\",\"levels\":[{\"lower_bound\":2,\"value\":2}]}]}]}],\"condition_note\":[{\"language_tag\":\"\",\"marketplace_id\":\"\",\"value\":\"43fedfddfdffd\"}],\"condition_type\":[{\"marketplace_id\":\"\",\"value\":\"new_oem\"}],\"skip_offer\":[{\"marketplace_id\":\"\",\"value\":false}],\"merchant_shipping_group\":[{\"marketplace_id\":\"\",\"value\":\"432efrddfgdf\"}],\"list_price\":[{\"currency\":\"USD\",\"marketplace_id\":\"\",\"value\":0.06}],\"main_offer_image_locator\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"gift_options\":[{\"can_be_messaged\":false,\"can_be_wrapped\":false,\"marketplace_id\":\"\"}],\"product_tax_code\":[{\"marketplace_id\":\"\",\"value\":\"43refdgdfg\"}],\"other_offer_image_locator_5\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_4\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_3\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_2\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"other_offer_image_locator_1\":[{\"marketplace_id\":\"\",\"media_location\":\"https://a.com\"}],\"fulfillment_availability\":[{\"fulfillment_channel_code\":\"AMAZON_NA\",\"quantity\":5,\"lead_time_to_ship_max_days\":2,\"restock_date\":\"2025-02-10\",\"is_inventory_available\":false}],\"merchant_release_date\":[{\"marketplace_id\":\"\",\"value\":\"2025-02-10\"}],\"max_order_quantity\":[{\"marketplace_id\":\"\",\"value\":10}]}";

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public String transform(JsonNode node) {
        ObjectNode jn = objectMapper.createObjectNode();
        jn.put("productType", "HARDWARE_CLAMP_VISE");
        ArrayNode an = objectMapper.createArrayNode();

        node.fieldNames().forEachRemaining(fn -> {
            ObjectNode on = objectMapper.createObjectNode();
            on.put("op", "replace");
            on.put("path", "/attrubites/" + fn);
            on.put("value", node.get(fn));
            an.add(on);
        });
        jn.put("patches", an);
        return jn.toPrettyString();
    }

    public static void main(String[] args) throws JsonProcessingException {
        PatchedJsonGenerator generator = new PatchedJsonGenerator();
        JsonNode formDataNode = objectMapper.readTree(PATCHED_FORM_DATA);
        System.out.println(generator.transform(formDataNode));
    }
}
